#!/usr/bin/env bash

# Copyright (c) 2016-2017 Bitnami
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Special case: if ./ksonnet-lib exists, set KUBECFG_JPATH
test -d $PWD/ksonnet-lib && export KUBECFG_JPATH=$PWD/ksonnet-lib

# We require below env
: ${GOPATH:?} ${KUBECFG_JPATH:?}
export PATH=${PATH}:${GOPATH}/bin

# Default kubernetes context - if it's "dind" or "minikube" will
# try to bring up a local (dockerized) cluster
test -n "${TRAVIS_K8S_CONTEXT}" && set -- ${TRAVIS_K8S_CONTEXT}
# minikube seems to be more stable than dind, sp for kafka
INTEGRATION_TESTS_CTX=${1:-minikube}

INTEGRATION_TESTS_TARGET=${2:-default}

# Check for some needed tools, install (some) if missing
which bats > /dev/null || {
   echo "ERROR: 'bats' is required to run these tests," \
        "install it from https://github.com/sstephenson/bats"
   exit 255
}

# Start a k8s cluster (minikube, dind) if not running
kubectl get nodes --context=${INTEGRATION_TESTS_CTX:?} || {
    cluster_up=./script/cluster-up-${INTEGRATION_TESTS_CTX}.sh
    test -f ${cluster_up} || {
        echo "FATAL: bringing up k8s cluster '${INTEGRATION_TESTS_CTX}' not supported"
        exit 255
    }
    ${cluster_up}
}

# Both RBAC'd dind and minikube seem to be missing rules to make kube-dns work properly
# add some (granted) broad ones:
kubectl --context=${INTEGRATION_TESTS_CTX:?} get clusterrolebinding kube-dns-admin >& /dev/null || \
    kubectl --context=${INTEGRATION_TESTS_CTX:?} create clusterrolebinding kube-dns-admin --serviceaccount=kube-system:default --clusterrole=cluster-admin

# Prep: load test library, save current k8s default context (and restore it at exit),
# as kubeless doesn't support --context
export TEST_CONTEXT=${INTEGRATION_TESTS_CTX}
source script/libtest.bash
trap k8s_context_restore 0
k8s_context_save

# Run the tests thru bats:
kubectl create namespace kubeless
case $INTEGRATION_TESTS_TARGET in
deployment)
    bats tests/deployment-tests.bats
    ;;
basic)
    bats tests/integration-tests.bats
    ;;
http)
    bats tests/integration-tests-http.bats
    ;;
cronjob)
    bats tests/integration-tests-cronjob.bats
    ;;
prebuilt_functions)
    bats tests/integration-tests-prebuilt.bats
    ;;
*)
    bats tests/deployment-tests.bats && \
    bats tests/integration-tests.bats && \
    bats tests/integration-tests-http.bats && \
    bats tests/integration-tests-cronjob.bats
    ;;
esac
exit_code=$?

# Just showing remaining k8s objects
kubectl get all --all-namespaces

if [ ${exit_code} -ne 0 -o -n "${TRAVIS_DUMP_LOGS}" ]; then
    echo "INFO: Build ERRORed, dumping logs: ##"
    for ns in kubeless default; do
        echo "### LOGs: namespace: ${ns} ###"
        kubectl get pod -n ${ns} -oname|xargs -I@ sh -xc "kubectl logs -n ${ns} @|sed 's|^|@: |'"
    done
    echo "INFO: Description"
    kubectl describe pod -l created-by=kubeless
    echo "INFO: LOGs: pod: kube-dns ###"
    kubectl logs -n kube-system -l k8s-app=kube-dns -c kubedns
    echo "INFO: LOGs: END"
fi
[ ${exit_code} -eq 0 ] && echo "INFO: $0: SUCCESS" || echo "ERROR: $0: FAILED"
exit ${exit_code}
# vim: sw=4 ts=4 et si
